package com.qdsg.ysg.doctor.ui.activity.mine;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hjq.shape.view.ShapeTextView;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.util.AES128;
import com.qdsg.ysg.doctor.util.MatchUtil;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 忘记密码
 * @Author: lzp
 * @CreateDate: 2022/6/30 16:31
 * @UpdateRemark: 更新说明
 */

public class ForgetActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_code)
    ShapeTextView btnCode;
    @BindView(R.id.btn_next)
    ShapeTextView btnNext;

    private Handler mHandler = new Handler();
    int second = 60;

    @Override
    protected void initViews() {
        tvTitle.setText("忘记密码");
    }

    @Override
    protected void initData() {
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_forget_pwd;
    }

    @OnClick({R.id.btn_code, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_code:
                if (etPhone.getText().toString().isEmpty()) {
                    ToastUtil.showToast(mContext, "请填写手机号");
                    return;
                } else if (!MatchUtil.isMobileNo(etPhone.getText().toString())) {
                    ToastUtil.showToast(mContext, "手机格式不正确");
                    return;
                }
                second = 60;
                getCode();
                break;
            case R.id.btn_next:
                if (validate()) {
                    validateInfo();
                }
                break;
            default:
                break;
        }
    }

    private boolean validate() {
        if (etAccount.getText().toString().isEmpty()) {
            ToastUtil.showToast(mContext, "请填写工号");
            return false;
        } else if (etPhone.getText().toString().isEmpty()) {
            ToastUtil.showToast(mContext, "请填写手机号");
            return false;
        } else if (etCode.getText().toString().isEmpty()) {
            ToastUtil.showToast(mContext, "请填写验证码");
            return false;
        }
        return true;
    }

    private void validateInfo() {
        try {
            RestProxy.getInstance().validateInfo(etCode.getText().toString(), AES128.encryptStringAES(etPhone.getText().toString(), "7t3e506jaa10xbd4"), AES128.encryptStringAES(etAccount.getText().toString(), "7t3e506jaa10xbd4"), new Observer<BaseResponse>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(BaseResponse baseResponse) {
                    Bundle bundle = new Bundle();
                    bundle.putString("doctorCode", etAccount.getText().toString());
                    startActivity(ResetPwdActivity.class, bundle);
                    finish();
                }

                @Override
                public void onError(Throwable e) {
                    ToastUtil.onError(ForgetActivity.this, e);
                }

                @Override
                public void onComplete() {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getCode() {
        btnCode.setEnabled(false);
        mHandler.postDelayed(runnable, 1000);
        try {
            RestProxy.getInstance().getCode(AES128.encryptStringAES(etPhone.getText().toString(), "7t3e506jaa10xbd4"), new Observer<BaseResponse>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(BaseResponse baseResponse) {

                }

                @Override
                public void onError(Throwable e) {
                    ToastUtil.onError(ForgetActivity.this, e);
                }

                @Override
                public void onComplete() {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            second--;
            if (second != 0) {
                mHandler.postDelayed(runnable, 1000);
                btnCode.setEnabled(false);
                btnCode.setText(second + "秒后重试");
            } else {
                btnCode.setText("发送验证码");
                btnCode.setEnabled(true);
            }
        }
    };
}
